//
//  GoodsCell.m
//  shoppingMall
//
//  Created by Bear on 2016/10/27.
//  Copyright © 2016年 dragontrail. All rights reserved.
//

#import "GoodsCell.h"
#import "UIColor+Hex.h"
#import "GoodsModel.h"
#import "UIButton+CustomStyle.h"
#import "GoodsManager.h"

@interface GoodsCell ()

@property (weak, nonatomic) IBOutlet UILabel *editNumLable;

@end

@implementation GoodsCell

-(void)setCellwithModel:(GoodsModel *)model{
    self.model = model;
    self.imgView.image = [UIImage imageNamed:model.imgUrl];
    self.goodsNameLable.text = model.goodsName;
    self.goodsInfoLable.text = model.goodsInfo;
    self.priceNowLable.text = [NSString stringWithFormat:@"￥%.2f",[model.price_now floatValue]];
    self.numLable.text = [NSString stringWithFormat:@"X %d",[model.num intValue]];
    self.editNumLable.text =  [NSString stringWithFormat:@"%d",[model.num intValue]];
    [self.selectBtn setSelected:model.isSelected];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.backgroundColor = [UIColor colorWithRed:250/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:1.0];
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.selectBtn goodsSelectStyle];
    [self.selectBtn addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:nil];
}



- (IBAction)setSelectedAction:(UIButton *)sender {

    sender.selected = !sender.selected;
        if (!sender.selected) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"CancelSomeGoodsSelected" object:nil userInfo:nil];
    };
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{

    [[GoodsManager defaultManager] takeGoodWithGoods:self.model IsSeleted:self.selectBtn.isSelected];
}

-(void)ChangeSytleWithStyle:(CellCustomStyle)style{

    if (style == CellEditStyle) {
        self.editNumView.hidden = NO;
        self.deleteBtn.hidden = NO;
        self.priceNowLable.hidden = YES;
        self.priceOldLable.hidden = YES;
        self.numLable.hidden = YES;
    }else if (style == CellNoEditSyle){
        self.editNumView.hidden = YES;
        self.deleteBtn.hidden = YES;
        self.priceNowLable.hidden = NO;
        self.priceOldLable.hidden = NO;
        self.numLable.hidden = NO;

    }
}

- (IBAction)addNumOfGoods:(id)sender {

    NSInteger number = [self.model.num integerValue];
    number ++;
    self.model.num = [NSNumber numberWithInteger:number];
    self.editNumLable.text = [NSString stringWithFormat:@"%d",[self.model.num intValue]];
}

- (IBAction)minusNumOfGoods:(id)sender {
    NSInteger number = [self.model.num integerValue];
    if (number == 1) {
        return;
    }
    number --;
    self.model.num = [NSNumber numberWithInteger:number];
    self.editNumLable.text = [NSString stringWithFormat:@"%d",[self.model.num intValue]];
}


-(void)drawRect:(CGRect)rect{
    //绘制分割线
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"f4f4f4"].CGColor);
    CGContextMoveToPoint(context, 0, 0);
    CGContextSetLineWidth(context, 10.0f);
    CGContextAddLineToPoint(context, rect.size.width, 0);
    CGContextStrokePath(context);

}

-(void)dealloc{
    [self.model removeObserver:self forKeyPath:@"isSelected"];
}
@end
